<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Dao;

use App\Model\UserRelation;
use Hyperf\Di\Annotation\Inject;

class UserRelationDao
{
    /**
     * @Inject
     * @var UserRelation
     */
    protected $userRelation;

    /**
     * 增加一个关注.
     * @param $userId
     * @param $followUserId
     */
    public function addFollow($userId, $followUserId): bool
    {
        $id = $this->userRelation->addOne($userId, $followUserId, 1);
        return true;
    }

    /**
     * 取消关注.
     * @param $userId
     * @param $followUserId
     */
    public function removeFollow($userId, $followUserId): bool
    {
        $this->userRelation->deleteOne($userId, $followUserId, 1);
        return true;
    }

    /**
     * 拉黑.
     * @param $userId
     * @param $blockUserId
     */
    public function addBlock($userId, $blockUserId): bool
    {
        $id = $this->userRelation->addOne($userId, $blockUserId, 2);
        return true;
    }

    /**
     * 取消拉黑.
     * @param $userId
     * @param $blockUserId
     */
    public function removeBlock($userId, $blockUserId): bool
    {
        $this->userRelation->deleteOne($userId, $blockUserId, 2);
        return true;
    }

    /**
     * 获取被关注数（粉丝数）.
     * @param $userId
     */
    public function getFollowedNumber($userId): int
    {
        return UserRelation::where('relation_user_id', $userId)
            ->where('relation_type', 1)->count('user_id');
    }

    /**
     * 获取关注数.
     * @param $userId
     */
    public function getFollowNumber($userId): int
    {
        return UserRelation::where('user_id', $userId)
            ->where('relation_type', 1)->count('relation_user_id');
    }

    /**
     * 检测用户关系是否存在.
     * @param $userId
     * @param $followUserId
     * @param mixed $relationType
     */
    public function checkRelationIsset($userId, $followUserId, $relationType = 1): bool
    {
        $ret = UserRelation::where('user_id', $userId)
            ->where('relation_user_id', $followUserId)
            ->where('relation_type', $relationType)->find();
        return boolval($ret);
    }

    /**
     * 获取关注列表.
     * @param $userId
     * @param mixed $page
     * @param mixed $limit
     * @param mixed $start
     */
    public function getFollowList($userId, $start, $limit): array
    {
        return UserRelation::where('user_id', $userId)
            ->where('relation_type', 1)
            ->offset($start)
            ->limit($limit)
            ->with('userName')
            ->get(['relation_user_id as user_id'])
            ->toArray();
    }

    /**
     * 获取粉丝列表.
     * @param $userId
     * @param mixed $page
     * @param mixed $limit
     * @param mixed $start
     */
    public function getFollowedList($userId, $start, $limit): array
    {
        return UserRelation::where('relation_user_id', $userId)
            ->where('relation_type', 1)
            ->offset($start)
            ->limit($limit)
            ->with('userName')
            ->get(['user_id'])
            ->toArray();
    }

    /**
     * 检测是否关注.
     * @param $userId
     * @param $relationUserId
     */
    public function checkIsFollowMe($userId, $relationUserId): bool
    {
        $ret = UserRelation::where('user_id', $userId)
            ->where('relation_user_id', $relationUserId)
            ->where('relation_type', '1')
            ->count();
        return boolval($ret);
    }
}
